home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / tnserv.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  117 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "config.h"
  4. #include "mbuf.h"
  5. #include "timer.h"
  6. #include "icmp.h"
  7. #include "netuser.h"
  8. #include "tcp.h"
  9. #include "telnet.h"
  10. #include "session.h"
  11. #include "ftp.h"
  12. #include "iface.h"
  13. #include "ax25.h"
  14. #include "lapb.h"
  15. #include "finger.h"
  16. #include "nr4.h"
  17. #ifdef    UNIX
  18. #include <string.h>
  19. #endif
  20.  
  21. struct tcb *tnet_tcb;
  22. tn1(argc,argv)
  23. char *argv[];
  24. {
  25.     struct socket lsocket;
  26.     extern int32 ip_addr;
  27.     void tnet_state();
  28.     void t_state(),rcv_char();
  29.  
  30.     /* Incoming Telnet */
  31.     lsocket.address = ip_addr;
  32.     if(argc < 2)
  33.         lsocket.port = TELNET_PORT;
  34.     else
  35.         lsocket.port = atoi(argv[1]);
  36.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,rcv_char,NULLVFP,tnet_state,0,(char *)NULL);
  37. }
  38. /* Handle incoming Telnet connect requests by creating a Telnet session,
  39.  * then change upcall vector so it behaves like an ordinary Telnet session.
  40.  * 
  41.  */
  42. /*ARGSUSED*/
  43. static void
  44. tnet_state(tcb,old,new)
  45. struct tcb *tcb;
  46. char old,new;
  47. {
  48.     struct telnet *tn;
  49.     struct session *s,*newsession();
  50.     void t_state();
  51.     char *a;
  52.  
  53.     switch(new){
  54.     case ESTABLISHED:
  55.         log(tcb,"open Telnet");
  56.         /* Allocate a session descriptor */
  57.         if((s = newsession()) == NULLSESSION){
  58.             printf("\007Incoming Telnet call from %s refused; too many sessions\n",
  59.              psocket(&tcb->conn.remote));
  60.             fflush(stdout);
  61.             sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  62.             close_tcp(tcb);
  63.             return;
  64.         }
  65.         a = inet_ntoa(tcb->conn.remote.address);
  66.         if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  67.             strcpy(s->name,a);
  68.         s->type = TELNET;
  69.         s->parse = send_tel;
  70.         /* Create and initialize a Telnet protocol descriptor */
  71.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  72.             printf("\007Incoming Telnet call refused; no space\n");
  73.             fflush(stdout);
  74.             sndmsg(tcb,"Call rejected; no space on remote system\n");
  75.             close_tcp(tcb);
  76.             s->type = FREE;
  77.             return;
  78.         }
  79.         tn->session = s;    /* Upward pointer */
  80.         tn->state = TS_DATA;
  81.         s->cb.telnet = tn;    /* Downward pointer */
  82.  
  83.         tcb->user = (char *)tn;    /* Upward pointer */
  84.         tn->tcb = tcb;        /* Downward pointer */
  85.         printf("\007Incoming Telnet session %lu from %s\n",
  86.          (long)(s - sessions),psocket(&tcb->conn.remote));
  87.         fflush(stdout);
  88.         tcb->s_upcall = t_state;
  89.         return;
  90.     case CLOSED:
  91.         /* This will only happen if the connection closed before
  92.          * the session was set up, e.g., if we refused it because
  93.          * there were too many sessions, or if the server is being
  94.          * shut down.
  95.          */
  96.         if(tcb == tnet_tcb)
  97.             tnet_tcb = NULLTCB;
  98.         del_tcp(tcb);
  99.         break;
  100.     }
  101. }
  102. /* Shut down Telnet server */
  103. tn0()
  104. {
  105.     if(tnet_tcb != NULLTCB)
  106.         close_tcp(tnet_tcb);
  107. }
  108. sndmsg(tcb,msg)
  109. struct tcb *tcb;
  110. char *msg;
  111. {
  112.     struct mbuf *bp;
  113.  
  114.     bp = qdata(msg,(int16)strlen(msg));
  115.     send_tcp(tcb,bp);
  116. }
  117.